package com.yuanmei.views;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuanmei.model.TickQuote;
import com.yuanmei.common.Utils;

import java.util.List;
import java.util.Locale;

public class TickQuoteListAdapter extends RecyclerView.Adapter<TickQuoteListAdapter.TickDetailViewHolder> {

    private List<TickQuote> mTickQuoteList;
    private ItemClickListener mClickListener;
//    private Context mContext;

    TickQuoteListAdapter(List<TickQuote> tqList) {
        this.mTickQuoteList = tqList;
//        mContext = context;
    }

    void setTickQuoteList(List<TickQuote> tqList) {
        this.mTickQuoteList = tqList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public TickDetailViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 加载布局 item_main_activity_rv.xml
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tick_deal_item,
                parent, false);
        return new TickDetailViewHolder(view);
    }

    private int mCurrentClickPosition = -1;
    void setCurrentClickPosition(int position) {
        mCurrentClickPosition = position;
    }

    @Override
    public void onBindViewHolder(@NonNull final TickDetailViewHolder holder, final int position) {
        if (mTickQuoteList != null) {
            TickQuote quote = mTickQuoteList.get(position);
            String detail = String.format(Locale.CHINA,
                    "%s    %.2f    %.0f",
                    Utils.formatStringFromTick(quote.getTick()),
                    quote.getPrice(),
                    quote.getVolume() / 100);
            holder.mTvTickDetail.setText(detail);
            if (quote.getType().equalsIgnoreCase("买盘")) {
                // 抓取的 up_arrow 的红色
                holder.mTvTickDetail.setTextColor(Color.rgb(237,19,18));
                holder.mIvTickType.setImageResource(R.drawable.up_arrow);
            } else {
                // 抓取的 down_arrow 的绿色
                holder.mTvTickDetail.setTextColor(Color.rgb(46,159,72));
                holder.mIvTickType.setImageResource(R.drawable.down_arrow);
            }

            if (mCurrentClickPosition == position) {
                holder.mTvTickDetail.setBackgroundColor(Color.LTGRAY);
            } else {
                holder.mTvTickDetail.setBackgroundColor(Color.TRANSPARENT);
            }

            holder.mTvTickDetail.setOnClickListener(v -> {
                if (mClickListener != null) {
                    mClickListener.onRecycleListItemClick(TickQuoteListAdapter.this, v, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (mTickQuoteList == null || mTickQuoteList.size() < 1) {
            return 0;
        }
        return mTickQuoteList.size();
    }

    class TickDetailViewHolder extends RecyclerView.ViewHolder {
        private View mItemView;
        private TextView mTvTickDetail;
        private ImageView mIvTickType;

        TickDetailViewHolder(View itemView) {
            super(itemView);
            mItemView = itemView;
            mTvTickDetail = mItemView.findViewById(R.id.deal_detail);
            mIvTickType = mItemView.findViewById(R.id.deal_type);
        }
    }

    void setClickListener(ItemClickListener clickListener) {
        mClickListener = clickListener;
    }

    // 点击事件接口
    public interface ItemClickListener {
        void onRecycleListItemClick(RecyclerView.Adapter adapter, View view, int position);
    }
}